import importlib
import random
import re
import string
import time

import aiohttp
import pymongo
from bson import ObjectId

def get_proxy():
    import random
    port = random.randint(24000, 24400)
    '''
    hc2.iinti.cn
    hc1.iinti.cn
    wz1.iinti.cn
    haproxy
    '''
    proxies = {'http': f'http://zheng123:zheng123@wz1.iinti.cn:{port}',
               'https': f'http://zheng123:zheng123@wz1.iinti.cn:{port}'}
    return proxies
# class get_aioproxy():
#     def __init__(self):
#         self.username = "zheng123"
#         self.password = "zheng123"
#         self.proxy_auth = aiohttp.BasicAuth(self.username, self.password)
#         self.host=f'http://zheng123:zheng123@wz1.iinti.cn:{random.randint(24000, 24400)}'
#         self.proxy={'proxy_auth':self.proxy_auth,
#                'host':self.host}

def get_aioproxy():
    username = "zheng123"
    password = "zheng123"
    proxy_auth = aiohttp.BasicAuth(username, password)
    return proxy_auth

class mongo_manager():

    def __init__(self, collect_name, client="mongodb://172.23.130.245:27017/", db="wx_read"):
        self.client = pymongo.MongoClient(client)
        self.db = self.client[db]
        self.collect = self.db[collect_name]

    def getCollect(self, collect_name: str) -> pymongo.collection.Collection:
        """
        创建集合
        :return:
        """
        if bool(collect_name):
            return self.db[collect_name]
        return self.collect

    def findById(self, id: str):
        myquery = {"_id": ObjectId(id)}
        return self.collect.find_one(myquery)

    def findAll(self, data=None):
        if bool(data):
            return self.collect.find(data)
        return self.collect.find()

    def findOne(self, data=None):
        if bool(data):
            return self.collect.find_one(data)
        return self.collect.find_one()

    def find_one_and_update(self, filter, update_data):
        # pymongo.ReturnDocument.AFTER 表示 返回修改后的文档
        result = self.collect.find_one_and_update(filter=filter, update={'$set': update_data},
                                                  # sort=[("articles_count", -1)],
                                                  return_document=pymongo.ReturnDocument.AFTER)
        return result

    def insertOne(self, data: dict):
        return self.collect.insert_one(data)

    def insertMany(self, datas: list):
        res=self.collect.insert_many(datas)
        return res.inserted_ids

    def updateOne(self, query, new_value):
        newvalues = {"$set": new_value}
        return self.collect.update_one(query, newvalues)

    def updateMany(self, query, new_value):
        newvalues = {"$set": new_value}
        return self.collect.update_many(query, newvalues)

    def pushOne(self, query, new_value):
        newvalues = {"$push": new_value}
        return self.collect.update_one(query, newvalues,upsert=True)
    # def deleteById(self,id):
    #     return self.collect.remove({"_id", ObjectId(id)})

    def deleteOne(self, myquery):
        return self.collect.delete_one(myquery)

    def deleteMany(self, myquery):
        return self.collect.delete_many(myquery)

    def close(self):
        self.client.close()

    def not_exist(self, query):
        if bool(query):
            data = self.collect.find_one(query)
            if data:
                return False
            return True
    def delete_repeat(self, name):
        pipeline = [
            {
                '$group': {
                    '_id': '${}'.format(name),
                    'count': {'$sum': 1},
                    'duplicates': {'$addToSet': '$_id'}
                }
            },
            {
                '$match': {
                    'count': {'$gt': 1}
                }
            }
        ]

        duplicate_documents = self.collect.aggregate(pipeline)

        for document in duplicate_documents:
            for duplicate_id in document['duplicates'][1:]:
                self.collect.delete_one({'_id': duplicate_id})

        return True

    def add_status_field(self):
        self.collect.update_many({}, {'$set': {'status': 1}})

    def delete_double(self):
        self.collect.aggregate([
            {
                "$group": {
                    "_id": "$title",
                    "unique_ids": {
                        "$addToSet": "$_id"
                    },
                    "count": {
                        "$sum": 1
                    }
                }
            },
            {
                "$match": {
                    "count": {"$gt": 1}
                }
            },
            {
                "$project": {
                    "title": "$_id",
                    "unique_ids": 1
                }
            },
            {
                "$unwind": "$unique_ids"
            },
            {
                "$sort": {
                    "unique_ids": 1
                }
            },
            {
                "$group": {
                    "_id": "$title",
                    "doc_id": {
                        "$first": "$unique_ids"
                    }
                }
            },
            {
                "$project": {
                    "_id": "$doc_id"
                }
            },
            {
                "$out": "mycollection_unique"
            }
        ])

def check_string(string):
    keywords = ["艺术-绘画", "原版书-英文原版", "原版书", "图画", "童书-漫画卡通", "漫画", "工具书", "童书", "教育学习", "绘画", "摄影", "书法", "影视", "音乐", "舞蹈", "视频", "艺术"]
    # for keyword in keywords:
    #     if keyword in string:
    #         return True
    # return False

    #any()函数会检查这个列表中是否存在至少一个True的元素，如果存在就返回True，否则返回False
    return any(keyword in string for keyword in keywords)
# print(check_string("{'name': '精品图画小说', 'href': 'https://weread.qq.com/web/category/100000'}"))

def is_english_or_punctuation(text):
    vv=text
    spaceless_text = "".join(vv.split())
    if len(spaceless_text) == 0:
        return 2
    pattern = re.compile('[\u4e00-\u9fa5]')
    for char in text:
        if pattern.search(text):
            return 1
        elif char in string.ascii_letters or char in string.punctuation or char==' ':
            pass
        else:
            return 3
    return 1